/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.ITMCarto;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class S2InputDataController implements Initializable {
    @FXML
    private AnchorPane RootPane;
    @FXML
    private TableView<BaseData> DataTableView;
    @FXML
    private TableColumn<BaseData, String> DataName;
    @FXML
    private TableColumn<BaseData, String> DataType;
    @FXML
    private TableColumn<BaseData, String> DataPath;
    @FXML
    private Button BtnDeleteData;
    @FXML
    private Button BtnInputData;
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnLast;
    @FXML
    private Button BtnNext;
    
    private List<BaseData> DataList=new ArrayList<>();
    private ObservableList<BaseData> BaseDataItemList =  FXCollections.observableArrayList();
    private int WhichBtn=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        BaseDataItemList.clear();
        DataList.clear();
        SetTableColumn();
    }    

    @FXML
    private void HandleBtnDeleteData(ActionEvent event) {
        int i=DataTableView.getSelectionModel().getFocusedIndex();
        if(i!=-1&&i>=0){
            BaseDataItemList.remove(i);
            DataTableView.setItems(BaseDataItemList);
        }
    }

    @FXML
    private void HandleBtnInputData(ActionEvent event) throws MalformedURLException, IOException, FileNotFoundException, InvalidFormatException {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("打开文件");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); 
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("Microsoft Excel 工作表", "*.xls;*.xlsx"),
                new FileChooser.ExtensionFilter("All Files", "*.*")
        );
        Stage s = new Stage(); 
        File file = fileChooser.showOpenDialog(s);
        if (file != null) {
            String DataFilePath=file.getPath();
            BaseData BD=new BaseData(DataFilePath);
            //BaseData BD=new BaseData("D:/aa.xlsx");
            BaseDataItemList.add(BD);
            DataTableView.setItems(BaseDataItemList);
        }
    }

    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        WhichBtn=0;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnLast(ActionEvent event) {
        WhichBtn=-1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnNext(ActionEvent event) {
        DataList.clear();
        BaseDataItemList.stream().forEach((BaseDataItemList1) -> {
            DataList.add(BaseDataItemList1);
        });
        WhichBtn=1;
        RootPane.getScene().getWindow().hide();
    }
    
    public void SetDataList(List<BaseData> tDataList){
        DataList=tDataList;
        BaseDataItemList.clear();
        BaseDataItemList.addAll(DataList);
        DataTableView.setItems(BaseDataItemList);
    }
    
    private void SetTableColumn(){
        DataName.setCellValueFactory(new PropertyValueFactory<>("DataName"));
        DataType.setCellValueFactory(new PropertyValueFactory<>("DataType"));
        DataPath.setCellValueFactory(new PropertyValueFactory<>("DataPath"));
    }

    public List<BaseData> getDataList() {
        return DataList;
    }

    public int getWhichBtn() {
        return WhichBtn;
    }
    
}
